import { createStore } from "../Redux/redux";
let initState = {
  counter: {
    Count: 1,
  },
  info: {
    name: "张三",
    age: 18,
  },
};
const countReducer = (state, action) => {
  switch (action.type) {
    case "addCount":
      return {
        Count: state.Count + 1,
      };

    case "minCount":
      return { Count: state.Count - 1 };
    default:
      return state;
  }
};
const infoReducer = (state, action) => {
  switch (action.type) {
    case "addAge":
      return {
        ...state,
        age: state.age + 1,
      };
    case "minAge":
      return {
        ...state,
        age: state.age - 1,
      };
    default:
      return state;
  }
};
const combinReducers = (reducers) => {
  const keys = Object.keys(reducers);
  return function (state = {}, action) {
    const nextState = {};
    keys.forEach((key) => {
      const reducer = reducers[key];
      const prev = state[key];
      const next = reducer(prev, action);
      nextState[key] = next;
      // nextState[key] = reducers[key](state[key], action);
    });
    return nextState;
  };
};
const reducer = combinReducers({
  counter: countReducer,
  info: infoReducer,
});
export const store = createStore(reducer, initState);
